//
//  PartyOrgRequestViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/18.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "PartyOrgRequestViewController.h"
#import "ShareOpenRequestModel.h"
#import "ShareOpenRequestTableViewCell.h"
#import "ZJShareClaimViewController.h"
#import "ZJShareIssueViewController.h"
@interface PartyOrgRequestViewController ()<UITableViewDataSource,UITableViewDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  title数组  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
@end

@implementation PartyOrgRequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"需求列表";
    if (self.isFromSecretary) {
        self.rightButton.hidden = NO;
    }
    
    [self.rightButton setImage:[UIImage imageNamed:@"footmark_addwhite"] forState:UIControlStateNormal];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}

- (void)action_rightButton:(UIButton *)sender{
    ZJShareIssueViewController *vc = [ZJShareIssueViewController new];
    vc.isSecretary = self.isFromSecretary;
    __weak typeof(self) weakSelf = self;
    vc.block = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer;
    NSString *urlStirng;
    
    NSString *UNITID = @"";
    NSString *pass = @"";
    if (self.isFromSecretary) {
        pass = @"1";
        UNITID = ZJUSERINFORMODEL.CURRENT_ROLE_MODEL.DEPT_ID;
    }else{
        pass = @"2";
        UNITID = self.UNITID;
    }
    paramer   = @{@"pageIndex": page,
                  @"pageSize": @"10",
                  @"UNITID":UNITID,
                  @"PASS":pass};

    
    urlStirng =[NetWorkingURL getSelfRequirementMobile];
    
    
    [NetWorkingManager postWithUrlString:urlStirng Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array;
            
            array = [object objectForKey:@"REQUIREMENT"];
            
            
            weakSelf.dataSouce =[[NSArray yy_modelArrayWithClass:[ShareOpenRequestModel class] json:array] mutableCopy];
            
            [self.tableView reloadData];
            if (weakSelf.dataSouce.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }
            
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer;
    NSString *urlStirng;
    
    NSString *pass = @"";
    NSString *UNITID = @"";
    if (self.isFromSecretary) {
        pass = @"1";
        UNITID = ZJUSERINFORMODEL.CURRENT_ROLE_MODEL.DEPT_ID;
    }else{
        pass = @"2";
         UNITID = self.UNITID;
    }
    paramer   = @{@"pageIndex": page,
                  @"pageSize": @"10",
                  @"UNITID":UNITID,
                  @"PASS":pass};
    urlStirng =[NetWorkingURL getSelfRequirementMobile];
    
    [NetWorkingManager postWithUrlString:urlStirng Parameter:paramer Success:^(id object) {
        NetWorkModel *mode = [NetWorkModel yy_modelWithJSON:object];
        if ([mode.STATE isEqualToString:@"20000"]) {
            NSArray *array;
            
            array = [object objectForKey:@"REQUIREMENT"];
            
            
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ShareOpenRequestModel class] json:array]];
            
            
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    header.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = header;
    
    
}




#pragma mark - /**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    
    ShareOpenRequestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ShareOpenRequestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
    
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJShareClaimViewController *vc = [ZJShareClaimViewController new];
    vc.model = self.dataSouce[indexPath.row];
    __weak typeof(self) weakSelf = self;
    vc.block = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    if (!self.isFromSecretary) {
        vc.isNoShow = YES;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



- (ZJRefurbish *)refurbish{
    if (!_refurbish ) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
